<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '起重机类型分布',
          textStyle: {
            color: 'yellow'
          },
          x: '5%',
          y: '5%'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: [
          {
            x: '65%',
            y: '22%',
            data: ['桥式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          },
          {
            x: '65%',
            y: '29%',
            data: ['塔式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          },
          {
            x: '65%',
            y: '36%',
            data: ['门式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }, {
            x: '65%',
            y: '43%',
            data: ['门座式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }, {
            x: '65%',
            y: '50%',
            data: ['流动式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }, {
            x: '65%',
            y: '57%',
            data: ['缆索式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }, {
            x: '65%',
            y: '64%',
            data: ['桅杆式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }, {
            x: '65%',
            y: '71%',
            data: ['架桥机'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }, {
            x: '65%',
            y: '78%',
            data: ['升降机'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          },
          {
            x: '65%',
            y: '85%',
            data: ['其他类型'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }
        ],
        series: [
          {
            name: '起重机类型分布',
            type: 'pie',
            radius: '100',
            center: ['40%', '60%'],
            itemStyle: {
              normal: {
                color: function(params) {
                  var colorList = [
                    '#C1232B', '#B5C334', '#FCCE10', '#E87C25', '#27727B',
                    '#FE8463', '#9BCA63', '#FAD860', '#F3A43B', '#60C0DD',
                    '#D7504B', '#C6E579', '#F4E001', '#F0805A', '#26C0C0'
                  ]
                  return colorList[params.dataIndex]
                },
                label: {
                  show: true,
                  position: 'top',
                  formatter: '{b} : {c}'
                }
              }
            },
            data: [
              { value: 2217, name: '桥式' },
              { value: 4, name: '塔式' },
              { value: 233, name: '门式' },
              { value: 49, name: '门座式' },
              { value: 8, name: '流动式' },
              { value: 1233, name: '缆索式' },
              { value: 1123, name: '桅杆式' },
              { value: 1101, name: '架桥机' },
              { value: 7, name: '升降机' },
              { value: 532, name: '其他类型' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
